TAKE AWAY POINTS FROM THIS POST
First we read in and organize the data.
library(dplyr)
library(ggmap)
# data = read.table("data_locations.txt", header=T, sep="\t") %>%
# mutate(address = as.character(address)) %>%
# rowwise() %>%
# mutate(longitude = geocode(address, source = "google")[1, 1]) %>%
# mutate(latitude = geocode(address, source = "google")[1, 2])
data = read.table("data_saved.txt", header=T, sep="\t")
Let’s view the data.
We then create a map object pulled from Google maps.
# Get city specific maps
paris_map = get_googlemap(center = "Paris", maptype = "roadmap", zoom = 12, size = c(640, 420))
berlin_map = get_googlemap(center = "Berlin", maptype = "roadmap", zoom = 11, size = c(640, 420))
barcelona_map = get_googlemap(center = "Barcelona", maptype = "roadmap", zoom = 12, size = c(640, 420))
prague_map = get_googlemap(center = "Prague", maptype = "roadmap", zoom = 12, size = c(640, 420))
With our map object saved
# Plot city specific maps with landmarks noted
city_plot = function(city_name, city_map){
ggmap(city_map, extent = "device") +
geom_point(data = subset(data, city == city_name), aes(x = longitude, y = latitude),
color = "red", size = 4)
}
paris.plot = city_plot("Paris", paris_map)
paris.plot
berlin.plot = city_plot("Berlin", berlin_map)
berlin.plot
barcelona.plot = city_plot("Barcelona", barcelona_map)
barcelona.plot
prague.plot = city_plot("Prague", prague_map)
prague.plot
With our maps and data points in place let’s compute the tessilation for each map.
library(deldir)
# data_deldir = data %>%
# group_by(city) %>%
# do(deldir(.$longitude, .$latitude)) %>%
# ungroup()
#
#
# data_deldir = data %>%
# nest(-city) %>%
# mutate(col = map2(longitude, latitude, deldir)) %>%
# select(city, col) %>%
# unnest()
### TO BE CHANGED
paris_data = filter(data, city == "Paris")
paris_deldir = deldir(paris_data$longitude, paris_data$latitude)
berlin_data = filter(data, city == "Berlin")
berlin_deldir = deldir(berlin_data$longitude, berlin_data$latitude)
barcelona_data = filter(data, city == "Barcelona")
barcelona_deldir = deldir(barcelona_data$longitude, barcelona_data$latitude)
prague_data = filter(data, city == "Prague")
prague_deldir = deldir(prague_data$longitude, prague_data$latitude)
Now we can update our figures with the tessliations.
del_plot = function(city_data, city_map) {
city_map +
geom_segment(data = city_data$delsgs, aes(x = x1, y = y1, xend = x2, yend = y2),
size = 1, color= "black")
}
paris_del.plot = del_plot(paris_deldir, paris.plot)
paris_del.plot
berlin_del.plot = del_plot(berlin_deldir, berlin.plot)
berlin_del.plot
barcelona_del.plot = del_plot(barcelona_deldir, barcelona.plot)
barcelona_del.plot
prague_del.plot = del_plot(prague_deldir, prague.plot)
prague_del.plot
Let’s compare the areas. The area for Paris is 0.004232, Berlin 0.011105, Barelona 0.002051, and Prague 0.002625.
Caculate centroid of the tesselation.
## TO BE CHANGED
paris_deldir_sum = data.frame(paris_deldir$summary) %>%
mutate(city = "Paris")
berlin_deldir_sum = data.frame(berlin_deldir$summary) %>%
mutate(city = "Berlin")
barcelona_deldir_sum = data.frame(barcelona_deldir$summary) %>%
mutate(city = "Barcelona")
prague_deldir_sum = data.frame(prague_deldir$summary) %>%
mutate(city = "Prague")
data_deldir_sum = paris_deldir_sum %>%
bind_rows(berlin_deldir_sum) %>%
bind_rows(barcelona_deldir_sum) %>%
bind_rows(prague_deldir_sum)
data_deldir_centroid = data_deldir_sum %>%
group_by(city) %>%
summarise(cent_x = sum(x * del.wts),
cent_y = sum(y * del.wts)) %>%
ungroup()
Plot figures with centroid noted.
# Make plots with centroid
cent_plot = function(city_name, city_map){
city_map +
geom_point(data = subset(data_deldir_centroid, city == city_name),
aes(x = cent_x, y = cent_y),
size = 8, color= "blue")
}
paris_del_cent.plot = cent_plot("Paris", paris_del.plot)
paris_del_cent.plot
berlin_del_cent.plot = cent_plot("Berlin", berlin_del.plot)
berlin_del_cent.plot
barcelona_del_cent.plot = cent_plot("Barcelona", barcelona_del.plot)
barcelona_del_cent.plot
prauge_del_cent.plot = cent_plot("Prague", prague_del.plot)
prauge_del_cent.plot